package com.api.oa.mapper;

import java.util.List;
import java.util.Map;

import com.api.oa.model.entity.OaNotice;
import com.api.oa.model.entity.OaOrgClassify;
import com.api.oa.model.vo.OaNoticeVo;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

@Repository
@Mapper
public interface OaOrgNoticeMapper {


	@Insert("insert into aoa_notice_list (" +
			"content," +
			"isTop," +
			"modifyTime," +
			"noticeTime," +
			"statusId," +
			"title," +
			"typeId," +
			"url," +
			"noticeOrgId," +
			"userId," +
			"isDraft," +
			"downFileId" +
			") " +
			"values " +
			"(" +
			"#{business.content}," +
			"#{business.isTop}," +
			"#{business.modifyTime}," +
			"#{business.noticeTime}," +
			"#{business.statusId}," +
			"#{business.title}," +
			"#{business.typeId}," +
			"#{business.url}," +
			"#{business.noticeOrgId}," +
			"#{business.userId}," +
			"#{business.isDraft}," +
			"#{business.downFileId}" +
			")" )
	@Options(useGeneratedKeys = true, keyProperty = "noticeId", keyColumn = "id")
	Long insert(@Param("business") OaNotice business);


	@Update("update aoa_notice_list set content = #{org.content}, modifyTime=#{org.modifyTime}," +
			"statusId = #{org.statusId}, typeId=#{org.typeId} ,isDraft = #{org.isDraft},downFileId=#{org.downFileId} where noticeId = #{org.noticeId}")
	Integer update(@Param("org") OaNotice org);


	@Delete("delete from aoa_notice_list where noticeId=#{id}")
	void delete(@Param("id") Long id);


	@Select("select * from aoa_notice_list where noticeId = #{pid}")
	List<OaNotice> list(@Param("pid") Long pid);


	@Select("select * from aoa_notice_list where noticeId = #{id} limit 1")
	OaNotice findOne(@Param("id") Long id);


	/**
	 * 发布者查看自己发布的消息
	 * @param orgId
	 * @param isDraft  0 草稿 1 发送
	 * @return
	 */
	@Select("<script>select * from  aoa_notice_list <if test='orgId!=null'>where noticeOrgId = #{orgId}</if> <if test='isDraft!=null'> and isDraft=#{isDraft}</if> order by noticeTime desc</script>")
	List<OaNotice> listFrom(@Param("orgId") Long orgId,@Param("isDraft") Long isDraft);


	@Select("<script>select * from (select * from aoa_notice_user_relation left JOIN oa_org on  aoa_notice_user_relation.relationOrgId = oa_org.orgId) d left join aoa_notice_list on aoa_notice_list.noticeId =  d.relationNoticeId " +
			"where isDraft = 1 and relationOrgId = #{orgId} <if test='isRead !=null'>and isRead = #{isRead} </if> order by noticeTime desc</script> ")
	List<OaNoticeVo> listTo(@Param("orgId") Long orgId, @Param("isRead") Integer isRead);
}
